﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IDAL;
using SD.Entities;
using System.Data.SqlClient;
using System.Data;

namespace SD.DAL
{
    public class TrustRegionDAO : BaseDAO, ITrustRegionDAO
    {
       /// <summary>
       /// Get trust region list by filter expression
       /// </summary>
       /// <param name="includeInactive"></param>
       /// <param name="filterExpression"></param>
       /// <returns></returns>
        public List<TrustRegion> GetByFilter(bool includeInactive, string filterExpression)
        {
            // Obtain SqlCommand object
            SqlCommand comm = CreateCommand();

            // Assign stored procedure
            comm.CommandText = "TrustRegionGetByFilter";

            // Add parameters to stored procedure
            comm.Parameters.AddWithValue("@includeInactive", includeInactive);
            comm.Parameters.AddWithValue("@filterExpression", filterExpression);

            DataTable dt;

            try
            {
                // Execute stored procedure
                dt = ExecuteSelectCommand(comm);
            }
            catch (SqlException ex)
            {                
                throw ex;
            }
           

            // Create new list trust region
            List<TrustRegion> list = new List<TrustRegion>();

            // Add each trust region in table to list
            foreach (DataRow row in dt.Rows)
            {
                TrustRegion tr = new TrustRegion();
                tr.TrustRegionID = row["TrustRegionID"].ToString();
                tr.Name = row["Name"].ToString();
                tr.Description = row["Description"].ToString();
                tr.Country = new CountryDAO().GetById(row["CountryID"].ToString()); 
                tr.IsActive = bool.Parse(row["IsActive"].ToString());

                list.Add(tr);
            }

            // return list trust region
            return list; 
        }
    }
}
